package com.yxkj.syh.app.huarong.activities.item_select;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.bindingAdp.edittext.SimpleTextWatcher;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.ItemSelectAdp;
import com.yxkj.syh.app.huarong.bean.Buyer;
import com.yxkj.syh.app.huarong.bean.Linkman;
import com.yxkj.syh.app.huarong.bean.PaperFactory;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityItemSelectBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 要素选择
 */
@Route(path = ArouterPath.ITEM_SELECT_ACTIVITY)
public class ItemSelectActivity extends BaseActivity<ActivityItemSelectBinding, ItemSelectVM> {

    public static final int SUPPLIER = 0;
    public static final int FACTORY = 1;

    @Autowired
    int type;

    ItemSelectAdp mItemSelectAdp;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_item_select;
    }

    @Override
    protected int getViewModelId() {
        return BR.itemSelectVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mVDBinding.titleView.setLeftOnClickListener(v -> {
            finish();
        });
        switch (type) {
            case SUPPLIER:
                mVDBinding.titleView.setTitleText("选择供应商");
                break;
            case FACTORY:
                mVDBinding.titleView.setTitleText("选择纸厂");
                break;
        }

        mItemSelectAdp = new ItemSelectAdp(this, showText -> {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            switch (type) {
                case SUPPLIER:
                    bundle.putSerializable("data", (Linkman) showText);
                    break;
                case FACTORY:
                    bundle.putSerializable("data", (PaperFactory) showText);
                    break;
            }
            resultIntent.putExtras(bundle);
            ItemSelectActivity.this.setResult(RESULT_OK, resultIntent);
            ItemSelectActivity.this.finish();
        });
        mVDBinding.rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mVDBinding.rvData.setAdapter(mItemSelectAdp);

        mVDBinding.etWord.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                refreshList(s.toString());
            }
        });
    }

    @Override
    protected void initData() {
        switch (type) {
            case SUPPLIER:
                mViewModel.linkList();
                break;
            case FACTORY:
                mViewModel.factoryList();
                break;
        }
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldShowText.observe(this, showTexts -> {
            refreshList(mVDBinding.etWord.getText().toString());
        });
    }

    private void refreshList(String keyWord) {
        List<ItemSelectAdp.ShowText> data = new ArrayList<>();
        if (mViewModel.mldShowText.getValue() != null) {
            for (ItemSelectAdp.ShowText walletAsset : (List<ItemSelectAdp.ShowText>) mViewModel.mldShowText.getValue()) {
                if ((TextUtils.isEmpty(keyWord) || walletAsset.getShowText().contains(keyWord))) {
                    data.add(walletAsset);
                }
            }
            mItemSelectAdp.setNewData(data);
        }
    }

}
